<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>管理员登录</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        .login-container {
            max-width: 400px;
            margin: 100px auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .btn-login {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .error-message {
            color: red;
            margin-bottom: 15px;
        }
        .login-type-switch {
            margin-bottom: 20px;
            text-align: center;
        }
        .login-type-switch button {
            padding: 8px 15px;
            margin: 0 5px;
            border: 1px solid #ddd;
            background: none;
            cursor: pointer;
        }
        .login-type-switch button.active {
            background-color: #007bff;
            color: white;
            border-color: #007bff;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h2>管理员登录</h2>
        <div th:if="${param.error}" class="error-message">
            用户名或密码错误
        </div>
        <div class="login-type-switch">
            <button onclick="switchToFormLogin()" id="formLoginBtn" class="active">表单登录</button>
            <button onclick="switchToTokenLogin()" id="tokenLoginBtn">Token登录</button>
        </div>
        
        <form id="formLogin" th:action="@{/admin/login}" method="post">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" required>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required>
            </div>
            <button type="submit" class="btn-login">登录</button>
        </form>
        
        <div id="tokenLogin" style="display: none;">
            <div class="form-group">
                <label for="token">管理员Token</label>
                <input type="text" id="token" placeholder="请输入Token">
            </div>
            <button onclick="loginWithToken()" class="btn-login">使用Token登录</button>
        </div>
    </div>

    <script>
        function switchToFormLogin() {
            document.getElementById('formLogin').style.display = 'block';
            document.getElementById('tokenLogin').style.display = 'none';
            document.getElementById('formLoginBtn').classList.add('active');
            document.getElementById('tokenLoginBtn').classList.remove('active');
        }
        
        function switchToTokenLogin() {
            document.getElementById('formLogin').style.display = 'none';
            document.getElementById('tokenLogin').style.display = 'block';
            document.getElementById('formLoginBtn').classList.remove('active');
            document.getElementById('tokenLoginBtn').classList.add('active');
        }
        
        function loginWithToken() {
            const token = document.getElementById('token').value;
            if (!token) {
                alert('请输入Token');
                return;
            }
            
            fetch('/api/admin/users', {
                headers: {
                    'Authorization': 'Bearer ' + token
                }
            })
            .then(response => {
                if (response.ok) {
                    localStorage.setItem('admin_token', token);
                    window.location.href = '/admin/dashboard';
                } else {
                    throw new Error('Token无效');
                }
            })
            .catch(error => {
                alert('Token验证失败：' + error.message);
            });
        }

        document.getElementById('formLogin').addEventListener('submit', function(e) {
        });
    </script>
</body>
</html> 